`
https://leetcode.cn/problems/maximum-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var constructMaximumBinaryTree = function (nums) {
  const build = (low, high) => {
    // 子数组没有元素，返回 null
    if (low > high) return null

    // 计算子数组的最大值的索引
    let idx = -1, maxVal = -Infinity
    for (let i = low; i <= high; i++) {
      if (nums[i] > maxVal) {
        maxVal = nums[i]
        idx = i
      }
    }

    // 创建结点，递归构建
    const node = new TreeNode(nums[idx])
    node.left = build(low, idx - 1)
    node.right = build(idx + 1, high)

    return node
  }

  return build(0, nums.length - 1)
};